package com.jiage.toolutil.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.telephony.TelephonyManager;

import com.jiage.managerlibrary.ActivityManager;

import java.io.File;
import java.io.IOException;
import java.util.List;

import androidx.core.content.FileProvider;

/**
 * package包工具类
 *
 * @author zhengjun
 */
public class SDPackageUtil {

    /**
     * 获取设备ID
     */
    public static String getDeviceId() {
        try {
            TelephonyManager tm = (TelephonyManager) ActivityManager.getInstance().getLastActivity().getSystemService(Context.TELEPHONY_SERVICE);
            String deviceId = tm.getDeviceId();
            return deviceId;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static void chmod(String permission, String path) {
        try {
            String command = "chmod " + permission + " " + path;
            Runtime runtime = Runtime.getRuntime();
            runtime.exec(command);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Boolean isPackageExist(String packageName) {
        PackageManager manager = ActivityManager.getInstance().getLastActivity().getPackageManager();
        List<PackageInfo> pkgList = manager.getInstalledPackages(0);
        for (PackageInfo pi : pkgList) {
            if (pi.packageName.equalsIgnoreCase(packageName)) {
                return true;
            }
        }
        return false;
    }


    public static PackageInfo getApkPackageInfo(String apkFilePath) {
        PackageManager pm = ActivityManager.getInstance().getLastActivity().getPackageManager();
        PackageInfo apkInfo = pm.getPackageArchiveInfo(apkFilePath, PackageManager.GET_META_DATA);
        return apkInfo;
    }

    public static PackageInfo getPackageInfo(String packageName) {
        PackageManager pm = ActivityManager.getInstance().getLastActivity().getPackageManager();
        PackageInfo apkInfo = null;
        try {
            apkInfo = pm.getPackageInfo(packageName, 0);
        } catch (NameNotFoundException e) {
        }
        return apkInfo;
    }

    public static PackageInfo getCurrentPackageInfo() {
        return getPackageInfo(getPackageName());
    }

    public static Boolean installApkPackage(String apkFilePath) {
        Intent intent =new Intent(Intent.ACTION_VIEW);
        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.N) {
            Uri contentUri = FileProvider.getUriForFile(ActivityManager.getInstance().getLastActivity(),getPackageName()+".provider",new File(apkFilePath));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(contentUri,"application/vnd.android.package-archive");
        }else{
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(Uri.fromFile(new File(apkFilePath)),"application/vnd.android.package-archive");
        }
        ActivityManager.getInstance().getLastActivity().startActivity(intent);
        return true;
    }

    public static Bundle getMetaData(Context context) {
        try {
            ApplicationInfo info = ActivityManager.getInstance().getLastActivity().getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
            return info.metaData;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getPackageName() {
        return ActivityManager.getInstance().getLastActivity().getPackageName();
    }

    public static void startAPP(String appPackageName) {
        try {
            Intent intent = ActivityManager.getInstance().getLastActivity().getPackageManager().getLaunchIntentForPackage(appPackageName);
            ActivityManager.getInstance().getLastActivity().startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void startCurrentApp() {
        startAPP(ActivityManager.getInstance().getLastActivity().getPackageName());
    }

    public static int getVersionCode() {
        PackageInfo pi = getCurrentPackageInfo();
        return pi.versionCode;
    }

    public static String getVersionName() {
        PackageInfo pi = getCurrentPackageInfo();
        return pi.versionName;
    }
    /**
     * 获取应用程序名称
     */
    public static synchronized String getAppName() {
        try {
            PackageManager packageManager = ActivityManager.getInstance().getLastActivity().getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    getPackageName(), 0);
            int labelRes = packageInfo.applicationInfo.labelRes;
            return ActivityManager.getInstance().getLastActivity().getResources().getString(labelRes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
